export default class Map {
  constructor(arr?: any[]) {
    arr = arr || [];
    arr.forEach(item=>{
      if (item.length === 2) {
        this.set(item[0],item[1])
      }
    })
  }
  private itemObj: any[] = [];
  public size: number = 0;
  public set(key: any, value: any): Map {
    var turple = this.findObj(key);
    if (turple) {
      turple && (turple[1] = value);
    } else {
      var arr: any[] = [key, value];
      this.itemObj.push(arr);
      this.size += 1;
    }
    return this;
  }
  // 私有方法 找到item
  private findObj(key: any): any {
    return this.itemObj.find((item) => {
      return item[0] === key;
    });
  }
  // 私有方法 找到index下标
  private findIndex(key: any): number {
    return this.itemObj.findIndex((item) => {
      return item[0] === key;
    });
  }
  public delete(key: any): boolean {
    var index = this.findIndex(key);
    if (index > -1) {
      this.itemObj.splice(index, 1);
      this.size -= 1;
    } else {
      return false;
    }
    return true;
  }
  public has(key: any): boolean {
    return this.itemObj.some(function (item) {
      return item[0] === key;
    });
  }
  public get(key: any): any {
    var arr = this.findObj(key);
    if (arr) {
      return arr[1];
    } else {
      return void 0;
    }
  }
  public clear(): boolean {
    this.itemObj.length = 0;
    this.size = 0;
    return true;
  }
  public keys(): any[] {
    var arr: any[] = [];
    this.itemObj.forEach((item) => {
      arr.push(item[0]);
    });
    return arr;
  }
  public values(): any[] {
    var arr: any[] = [];
    this.itemObj.forEach((item) => {
      arr.push(item[1]);
    });
    return arr;
  }
  public forEach(handler: Function): void {
    this.itemObj.forEach(function () {
      handler(...arguments);
    });
  }
  public entries(): any {
    var nextIndex = 0;
    return {
      next: () => {
        return nextIndex < this.itemObj.length
          ? { value: this.itemObj[nextIndex++], done: false }
          : { value: undefined, done: true };
      },
    };
  }
}